package com.yimi.yinhepda.views.createPackage_module;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.BuildPckScanTableDao;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.DateHelper;
import com.yimi.yinhepda.utils.IntentUtils;
import com.pda.uce.commom.util.LogUtil;
import com.yimi.yinhepda.base.BaseInitActivity;
import java.util.Date;

/**
 * 建包操作页面
 *
 * @author tianjin
 */
public class CreatePackageOperatorActivity extends BaseInitActivity {

  /** 标题栏左边 */
  @Bind(R.id.title) TextView mTvTitle;
  /** 标题栏中间 */
  @Bind(R.id.num) TextView mTvNum;
  @Bind(R.id.ll_query_dept) LinearLayout mQueryDept;
  @Bind(R.id.tv_deptName) TextView mTvDeptName;

  /***
   * 数据库操作者
   */
  BuildPckScanTableDao mBuildPckScanTableDao;
  private String mDestDeptCode;

  public CreatePackageOperatorActivity() {
  }

  @Override public String getPageName() {
    return getString(R.string.page_create_operator);
  }

  @Override public int getContentViewId() {
    return R.layout.activity_create_package;
  }

  @Override public void initView(Bundle savedInstanceState) {
    mTvTitle.setText("建包操作");

    //返回按钮
    Button backImgLayout =  findViewById(R.id.activity_create_package_back);
    backImgLayout.setOnClickListener(this);
    //确定按钮
    Button okImageLayout =  findViewById(R.id.activity_create_package_ok);
    okImageLayout.setOnClickListener(this);
  }

  @Override public void initEvent() {
    mQueryDept.setOnClickListener(this);
  }

  @Override public void initData() {

    //查询未上传数据
    mBuildPckScanTableDao = new BuildPckScanTableDao();
  }

  @Override protected void onResume() {
    super.onResume();
    /** 查询未上传数据*/
    int totalNotUploadNum = new BuildPckScanTableDao().queryNotUploadDataNum();
    mTvNum.setText(totalNotUploadNum + "");
  }

  @Override public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {
      case R.id.activity_create_package_back:
        finish();
        break;
      case R.id.activity_create_package_ok:
        String destnation = mTvDeptName.getText().toString().trim();
        if (TextUtils.isEmpty(destnation)) {
          showToast("请输入目的地");
          return;
        }
        Intent intent = new Intent(CreatePackageOperatorActivity.this, CreatePackageScanActivity.class);
        intent.putExtra(IntentUtils.BUILD_PKG_TASK_NO, getTaskNo());
        intent.putExtra(IntentUtils.BUILD_DEST, mDestDeptCode);
        intent.putExtra(IntentUtils.BUILD_DEST_NAME, destnation);
        startActivity(intent);
        finish();
        break;
      case R.id.ll_query_dept:
        startActivityForResult(new Intent(this, CreateDeptSelectActivity.class), 0);
        break;
      default:
        break;
    }
  }

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
      mDestDeptCode = data.getStringExtra("deptCode");
      String mDeptName = data.getStringExtra("deptName");
      mTvDeptName.setText(mDeptName);
    }
  }

  @Override protected void onDestroy() {
    super.onDestroy();
  }

  /**
   * 获取任务号： 出发网点code+目的网点code+年2位+月2位+日2位+流水2位
   */
  private String getTaskNo() {
    String deptCode = sharedPreferUtil.getValue(Common.DEPT_CODE);
    String currDateNum = DateHelper.DateToStr(new Date(), DateHelper.dateFormatShort);
    String oldDateNum = sharedPreferUtil.getValue(Common.DATE_NUM);
    int buildPckNum = 0;
    if (currDateNum.equals(oldDateNum)) {
      buildPckNum = sharedPreferUtil.getIntValue(Common.BUILD_PCK_NUM) + 1;
      sharedPreferUtil.setIntValue(Common.BUILD_PCK_NUM, buildPckNum);
    } else {
      buildPckNum++;
      sharedPreferUtil.setValue(Common.DATE_NUM, currDateNum);
      sharedPreferUtil.setIntValue(Common.BUILD_PCK_NUM, buildPckNum);
    }
    String numStr = "";
    if (buildPckNum < 10) {
      numStr = "0" + buildPckNum;
    } else {
      numStr = buildPckNum + "";
    }
    String taskNo = deptCode + mDestDeptCode + currDateNum + numStr;
    LogUtil.e("taskNo----->" + taskNo);
    return taskNo;
  }
}
